package minh.bac.wcbrazil2014.homnay;

import java.util.ArrayList;
import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class HomNayFootBallDT extends Fragment {

	ImageView imgTeam;
	CheckBox favorite;	
	TextView doi,tucachthamdu,xephang,lichsuthamdu,thumon,hauve,tienve,tiendao;
	ArrayList<FootBallTeamObj> footBallTeamObjs;
	ImageButton backButton;
	private MySQLiteManager mMySQLiteManager;
	String nameTeam;
	String imgLinkTeam;
	DisplayImageOptions displayImageOptions;
	public static HomNayFootBallDT newInstance(String nameTeam, String imgLinkTeam)
	{
		HomNayFootBallDT deTailTeamFM = new HomNayFootBallDT();
		Bundle bundle = new Bundle();
		bundle.putString(WCTAG.KEY_NAMETEAM, nameTeam);
		bundle.putString(WCTAG.KEY_IMGLINK_TEAM, imgLinkTeam);
		deTailTeamFM.setArguments(bundle);
		return deTailTeamFM;		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.team_detail, container, false);
		//
		
		nameTeam = getArguments().getString(WCTAG.KEY_NAMETEAM);
		imgLinkTeam = getArguments().getString(WCTAG.KEY_IMGLINK_TEAM);
		
		//
		mMySQLiteManager = new MySQLiteManager(getActivity());
		doi = (TextView) rootView.findViewById(R.id.doibong_team);
		tucachthamdu = (TextView) rootView.findViewById(R.id.tucachthamdu_team);
		xephang = (TextView) rootView.findViewById(R.id.xephangfifa_team);
		lichsuthamdu = (TextView) rootView.findViewById(R.id.lichsuthamdu_team);
		thumon = (TextView) rootView.findViewById(R.id.thumon_team);
		hauve = (TextView) rootView.findViewById(R.id.hauve_team);
		tienve = (TextView) rootView.findViewById(R.id.tienve_team);
		tiendao = (TextView) rootView.findViewById(R.id.tiendao_team);
		imgTeam = (ImageView) rootView.findViewById(R.id.imageDoiBong_team);
		favorite = (CheckBox) rootView.findViewById(R.id.favoriteStar_team);
		backButton = (ImageButton) rootView.findViewById(R.id.imageButtonBack_team);
		footBallTeamObjs = new ArrayList<FootBallTeamObj>();
		displayImageOptions = new DisplayImageOptions.Builder()
		.cacheInMemory(false)
		.cacheOnDisc(true)		
		.resetViewBeforeLoading(false)
		.bitmapConfig(Bitmap.Config.RGB_565)										
		.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
		.showImageOnFail(R.drawable.ic_emblem_club_placeholder)		
		.imageScaleType(ImageScaleType.EXACTLY)										
		.build();
		LoadData();
		favorite.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					mMySQLiteManager.updateFavorite(nameTeam, 1);
				}
				else
					if(!isChecked)
						mMySQLiteManager.updateFavorite(nameTeam, 0);
			}
		});
		backButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				HomNayFragment.switchFragment(R.id.container_homnay, new HomNayFragment() , true , WCTAG.TAGFM_HOMEPL);
			}
		});
		return rootView;
	}
	public void LoadData()
	{
		
		
		footBallTeamObjs = mMySQLiteManager.getFootBallTeamByName(nameTeam);
		//
		doi.setText(footBallTeamObjs.get(0).getDoiBong());
		tucachthamdu.setText("- " + footBallTeamObjs.get(0).getTuCachThamDu());
		xephang.setText("- " + footBallTeamObjs.get(0).getXepHangFiFa());
		lichsuthamdu.setText("- " + footBallTeamObjs.get(0).getLichSuThamDu());
		thumon.setText("- " + footBallTeamObjs.get(0).getThuMon());
		hauve.setText("- " + footBallTeamObjs.get(0).getHauVe());
		tienve.setText("- " + footBallTeamObjs.get(0).getTienVe());
		tiendao.setText("- " + footBallTeamObjs.get(0).getTienDao());
		//
		ImageLoader.getInstance().displayImage(imgLinkTeam, imgTeam, displayImageOptions);
		if(footBallTeamObjs.get(0).getFavorite() == 1)
		{
			favorite.setChecked(true);
		}
		else
			favorite.setChecked(false);
		
	}
}
